package com.simple.datax.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IDataxFlowActorsBO;
import com.simple.datax.bo.IDataxFlowActorsTemplateBO;
import com.simple.datax.bo.IDataxFlowBO;
import com.simple.datax.bo.IDataxFlowTemplateBO;
import com.simple.datax.bo.IVwDataxEnterpriseBO;
import com.simple.datax.vo.DataxDictionaryVO;
import com.simple.datax.vo.DataxFlowActorsIdVO;
import com.simple.datax.vo.DataxFlowActorsTemplateVO;
import com.simple.datax.vo.DataxFlowActorsVO;
import com.simple.datax.vo.DataxFlowIdVO;
import com.simple.datax.vo.DataxFlowTemplateVO;
import com.simple.datax.vo.DataxFlowVO;
import com.simple.datax.vo.VwDataxEnterpriseVO;
import com.simple.util.common.Constants;
import com.simple.util.common.StringUtil;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.PageInfoBean;

@Controller 
public class CompanyFlowController extends AbstractDataxBaseController {
	
	private final static String JSP_PATH="_CompanyFlow";
	@Autowired
	private IDataxFlowBO dataxFlowBO;
	@Autowired
	private IDataxFlowTemplateBO dataxFlowTemplateBO;
	@Autowired
	private IDataxFlowActorsTemplateBO dataxFlowActorsTemplateBO;
	@Autowired
	private IDataxFlowActorsBO dataxFlowActorsBO;
	@Autowired
	private IDataxDictionaryBO dataxDictionaryBO;
	@Autowired
	private IVwDataxEnterpriseBO vwDataxEnterpriseBO;
	@RequestMapping("/companyFlow")  
    public String init(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    				   @RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
    				   @RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
    				   @RequestParam(value="eNameSelect",required=false) String eNameSelect,
    				   @RequestParam(value="statusSelect",required=false) String statusSelect,
    				   HttpServletRequest request, HttpServletResponse response)  {
		Log.info("Start CompanyFlowController.init ");
        Map<String, String> optionStatusMap = Constants.TEMPLATE_STATE_MAP;
        request.setAttribute("optionStatusMap", optionStatusMap);
        final PageInfoBean pageInfoBean = new PageInfoBean(pageNo);
		final List<DataxFlowVO> dataxFlowVOList = dataxFlowBO.findDataxFlowVOByPage(pageInfoBean, flowIdSelect, originateCopSelect, eNameSelect,statusSelect);
		request.setAttribute("dataxFlowVOList", dataxFlowVOList);
		 
		final String pageInfo = super.generatePageInfo(request,response,pageInfoBean, "goPage");
	    request.setAttribute("pageInfo", pageInfo);  
	    
	    request.setAttribute("pageNo", pageNo); 
	    request.setAttribute("flowIdSelect", flowIdSelect);  
	    request.setAttribute("originateCopSelect", originateCopSelect);  
	    request.setAttribute("eNameSelect", eNameSelect); 
	    request.setAttribute("statusSelect", statusSelect); 
	    
        return getJspPath("company_flow");
    }
	
	@RequestMapping("/companyFlowAdd")  
    public String add(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
			   		@RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
			   		@RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
			   		@RequestParam(value="eNameSelect",required=false) String eNameSelect,
			   		@RequestParam(value="statusSelect",required=false) String statusSelect,
			   		HttpServletRequest request, HttpServletResponse response){
		List<DataxFlowTemplateVO> optionFlowTemplate = dataxFlowTemplateBO.findAllActive();
		request.setAttribute("optionFlowTemplate", optionFlowTemplate);  
	    request.setAttribute("pageNo", pageNo);
	    request.setAttribute("flowIdSelect", flowIdSelect); 
	    request.setAttribute("originateCopSelect", originateCopSelect);  
	    request.setAttribute("eNameSelect", eNameSelect); 
	    request.setAttribute("statusSelect", statusSelect); 
	    
		request.setAttribute("isSelect", false); 
        return getJspPath("company_flow_add");
    }
	
	@RequestMapping(value="/companyFlowAddSubmit",method=RequestMethod.POST)  
    public String addSubmit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
				   			@RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
				   			@RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
				   			@RequestParam(value="eNameSelect",required=false) String eNameSelect,
				   			@RequestParam(value="statusSelect",required=false) String statusSelect,
				   			@RequestParam(value="listSize",required=false) String listSize,
				   			@RequestParam(value="flowId",required=false) String flowId,
				   			@ModelAttribute("DataxFlowVO") DataxFlowVO dataxFlowVO,
				   			HttpServletRequest request, HttpServletResponse response)  {
		Log.info("Start CompanyFlowController.addSubmit ");
		//check flowName unique
		final List<JFValidationBean> errorList = getErrorList();
		
		String flowName=dataxFlowVO.getFlowName();
		String flowTemplateId=dataxFlowVO.getId().getFlowTemplateId();
		if(!StringUtil.isNull(flowName)){
			if(dataxFlowBO.checkExistFlowName(flowName)){
				errorList.add(new JFValidationBean("message.companyfow.flowname.is.exist"));
			}
		}
		VwDataxEnterpriseVO VwDataxEnterpriseVO= vwDataxEnterpriseBO.findById(dataxFlowVO.getOriginateCop());
		if(VwDataxEnterpriseVO==null){
			errorList.add(new JFValidationBean("message.companyfow.enterprise.not.exist"));
		}
		
		if(errorList.isEmpty()){
			// init DataxFlowVO
			dataxFlowVO.getId().setFlowId(flowId);
			DataxFlowTemplateVO dataxFlowTemplateVO = dataxFlowTemplateBO
					.findById(dataxFlowVO.getId().getFlowTemplateId());
			dataxFlowVO.setCreatTime(dataxFlowTemplateVO.getCreatTime());
			dataxFlowVO.setStatus(dataxFlowTemplateVO.getStatus());
			dataxFlowVO.setUserId(dataxFlowTemplateVO.getCreater());
	
			List<DataxFlowActorsVO> listVo = new ArrayList<DataxFlowActorsVO>();
			if (!StringUtil.isNull(listSize)) {
				int iSize = Integer.parseInt(listSize);
				String actorNo = "actorNo_";
				String actorName = "actorName_";
				String ecCode = "ecCode_";
				String serviceCode = "serviceCode_";
				for (int i = 1; i <= iSize; i++) {
					DataxFlowActorsVO dataxFlowActorsVO = new DataxFlowActorsVO();
					DataxFlowActorsIdVO dataxFlowActorsIdVO = new DataxFlowActorsIdVO();
					dataxFlowActorsIdVO.setActorNo(request
							.getParameter(actorNo + i));
					dataxFlowActorsIdVO.setFlowId(flowId);
					dataxFlowActorsVO.setId(dataxFlowActorsIdVO);
					dataxFlowActorsVO.setActorName(request.getParameter(actorName
							+ i));
					dataxFlowActorsVO.seteCode(request.getParameter(ecCode + i));
					dataxFlowActorsVO.setServiceCode(request
							.getParameter(serviceCode + i));
					listVo.add(dataxFlowActorsVO);
				}
				dataxFlowBO.add(dataxFlowVO, listVo);
			}
		}
		
		final String strPath;
		if(errorList.isEmpty()){
			strPath=init(pageNo, flowIdSelect, originateCopSelect, eNameSelect,statusSelect,
					request, response);
		}else{
			strPath=showFlowTempInfo(flowTemplateId,pageNo, flowIdSelect, originateCopSelect, eNameSelect,statusSelect,flowId,
					dataxFlowVO,request, response);
		}
		return strPath;
    }
	
	@RequestMapping(value="/companyFlowEdit",method=RequestMethod.POST)  
	public String edit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
			   @RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
			   @RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
			   @RequestParam(value="eNameSelect",required=false) String eNameSelect,
			   @RequestParam(value="statusSelect",required=false) String statusSelect,
			   @RequestParam(value="flowId",required=false) String flowId,
			   @RequestParam(value="flowTemplateId",required=false) String flowTemplateId,
			   HttpServletRequest request, HttpServletResponse response)  {

		request.setAttribute("pageNo", pageNo);  
		request.setAttribute("flowIdSelect", flowIdSelect);  
		request.setAttribute("originateCopSelect", originateCopSelect);  
		request.setAttribute("eNameSelect", eNameSelect);
		request.setAttribute("statusSelect", statusSelect); 
		
		DataxFlowTemplateVO  dataxFlowTemplateVO=dataxFlowTemplateBO.findById(flowTemplateId);
		request.setAttribute("dataxFlowTemplateVO", dataxFlowTemplateVO); 
		DataxFlowIdVO dataxFlowIdVO =new DataxFlowIdVO();
		dataxFlowIdVO.setFlowId(flowId);
		dataxFlowIdVO.setFlowTemplateId(flowTemplateId);
		DataxFlowVO dataxFlowVO=dataxFlowBO.findById(dataxFlowIdVO);
		request.setAttribute("dataxFlowVO",dataxFlowVO); 
		List<Object> listFlowActors=dataxFlowActorsBO.findByFlowId(flowId);
		request.setAttribute("listFlowActors",listFlowActors); 
		request.setAttribute("listSize",listFlowActors.size()); 
		List<DataxDictionaryVO> listServiceType= dataxDictionaryBO.findTransServiceTypeDataxDictionaryVO();
		request.setAttribute("listServiceType",listServiceType); 
		return getJspPath("company_flow_edit");
}

	@RequestMapping(value="/companyFlowEditSubmit",method=RequestMethod.POST)  
    public String editSubmit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
				   			@RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
				   			@RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
				   			@RequestParam(value="eNameSelect",required=false) String eNameSelect,
				   			@RequestParam(value="statusSelect",required=false) String statusSelect,
				   			@RequestParam(value="listSize",required=false) String listSize,
				   			@RequestParam(value="notes",required=false) String notes,
				   			@ModelAttribute("DataxFlowIdVO") DataxFlowIdVO dataxFlowIdVO,
				   			HttpServletRequest request, HttpServletResponse response)  {
		Log.info("Start CompanyFlowController.editSubmit ");
		//check flowName unique
		final List<JFValidationBean> errorList = getErrorList();
		
		DataxFlowVO dataxFlowVO=dataxFlowBO.findById(dataxFlowIdVO);
		if(dataxFlowVO==null){
			errorList.add(new JFValidationBean("message.companyfow.dataxflowvo.not.exist"));
		}

		List<DataxFlowActorsVO> listVo = new ArrayList<DataxFlowActorsVO>();
		if (!StringUtil.isNull(listSize)) {
			int iSize = Integer.parseInt(listSize);
			String actorNo = "actorNo_";
			String ecCode = "ecCode_";
			String serviceCode = "serviceCode_";
			for (int i = 1; i <= iSize; i++) {
				
				DataxFlowActorsIdVO dataxFlowActorsIdVO = new DataxFlowActorsIdVO();
				dataxFlowActorsIdVO.setActorNo(request
						.getParameter(actorNo + i));
				dataxFlowActorsIdVO.setFlowId(dataxFlowIdVO.getFlowId());
				
				DataxFlowActorsVO dataxFlowActorsVO = dataxFlowActorsBO.findById(dataxFlowActorsIdVO);
				if(dataxFlowActorsVO==null){
					errorList.add(new JFValidationBean("message.companyfow.dataxflowactorvo.not.exist"));
				}else{
					dataxFlowActorsVO.seteCode(request.getParameter(ecCode + i));
					dataxFlowActorsVO.setServiceCode(request
							.getParameter(serviceCode + i));
					listVo.add(dataxFlowActorsVO);
				}
			}
		}
		if(errorList.isEmpty()){
			// init DataxFlowVO
			if(!StringUtil.isNull(notes)){
				dataxFlowVO.setNotes(notes);
			}
			dataxFlowBO.add(dataxFlowVO, listVo);
		}
		
		final String strPath;
		if(errorList.isEmpty()){
			strPath=init(pageNo, flowIdSelect, originateCopSelect, eNameSelect,statusSelect,
					request, response);
		}else{
			strPath=edit(pageNo, flowIdSelect, originateCopSelect, eNameSelect,statusSelect,dataxFlowIdVO.getFlowId(),
					dataxFlowIdVO.getFlowTemplateId(),request, response);
		}
		return strPath;
    }
	
	
	@RequestMapping(value="/companyDeleteFlow",method=RequestMethod.POST)  
    public String deleteFlow(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
				   			@RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
				   			@RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
				   			@RequestParam(value="eNameSelect",required=false) String eNameSelect,
				   			@RequestParam(value="statusSelect",required=false) String statusSelect,
				   			@RequestParam(value="flowId",required=false) String flowId,
				   			@RequestParam(value="flowTemplateId",required=false) String flowTemplateId,
				   			HttpServletRequest request, HttpServletResponse response)  {
		DataxFlowIdVO dataxFlowIdVO=new DataxFlowIdVO();
		dataxFlowIdVO.setFlowId(flowId);
		dataxFlowIdVO.setFlowTemplateId(flowTemplateId);
		dataxFlowBO.del(dataxFlowIdVO);
	    return init(pageNo, flowIdSelect, originateCopSelect, eNameSelect,statusSelect, request, response);
    }
	@RequestMapping(value="/companyFlowView",method=RequestMethod.POST)  
    public String view(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
					   @RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
					   @RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
					   @RequestParam(value="eNameSelect",required=false) String eNameSelect,
					   @RequestParam(value="statusSelect",required=false) String statusSelect,
					   @RequestParam(value="flowTemplateId",required=false) String flowTemplateId,
					   @RequestParam(value="flowId",required=false) String flowId,
    				   HttpServletRequest request, HttpServletResponse response)  {
		
	    request.setAttribute("pageNo", pageNo);  
	    request.setAttribute("flowIdSelect", flowIdSelect);  
	    request.setAttribute("originateCopSelect", originateCopSelect);  
	    request.setAttribute("eNameSelect", eNameSelect);
	    request.setAttribute("statusSelect", statusSelect); 
	    
	 	DataxFlowTemplateVO  dataxFlowTemplateVO=dataxFlowTemplateBO.findById(flowTemplateId);
	 	request.setAttribute("dataxFlowTemplateVO", dataxFlowTemplateVO); 
	 	DataxFlowIdVO dataxFlowIdVO =new DataxFlowIdVO();
	 	dataxFlowIdVO.setFlowId(flowId);
	 	dataxFlowIdVO.setFlowTemplateId(flowTemplateId);
		DataxFlowVO dataxFlowVO=dataxFlowBO.findById(dataxFlowIdVO);
		request.setAttribute("dataxFlowVO",dataxFlowVO); 
		List<Object> listFlowActors=dataxFlowActorsBO.findByFlowId(flowId);
		request.setAttribute("listFlowActors",listFlowActors); 
		List<DataxDictionaryVO> listServiceType= dataxDictionaryBO.findTransServiceTypeDataxDictionaryVO();
		request.setAttribute("listServiceType",listServiceType); 
        return getJspPath("company_flow_view");
    }
	

	
	@RequestMapping("/showFlowTempInfo")
	public String showFlowTempInfo(@RequestParam(value="flowTempIdSelect",required=false) String flowTempIdSelect,
								@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
					   			@RequestParam(value="flowIdSelect",required=false) String flowIdSelect,
					   			@RequestParam(value="originateCopSelect",required=false) String originateCopSelect,
					   			@RequestParam(value="eNameSelect",required=false) String eNameSelect,
					   			@RequestParam(value="statusSelect",required=false) String statusSelect,
					   			@RequestParam(value="flowId",required=false) String flowId,
					   			@ModelAttribute("DataxFlowVO") DataxFlowVO dataxFlowVO,
								HttpServletRequest request, HttpServletResponse response){
		 	DataxFlowTemplateVO  dataxFlowTemplateVO=dataxFlowTemplateBO.findById(flowTempIdSelect);
		 	request.setAttribute("dataxFlowTemplateVO", dataxFlowTemplateVO); 
		 	List<DataxFlowActorsTemplateVO> flowActorsTemplateVOList=dataxFlowActorsTemplateBO.findByFlowTempId(flowTempIdSelect);
		 	//set flowId
		 	String strFlowId=flowId;
		 	if(StringUtil.isNull(strFlowId)){
		 		strFlowId="E"+dataxFlowTemplateVO.getFlowTemplateId()+StringUtil.leftPad(dataxFlowBO.generateId(),"0",8);
		 	}
		 	dataxFlowVO.getId().setFlowId(strFlowId);
		 	request.setAttribute("flowActorsTemplateVOList", flowActorsTemplateVOList); 
		 	request.setAttribute("listSize", flowActorsTemplateVOList.size()); 
		 	request.setAttribute("isSelect", true);
		 	List<DataxFlowTemplateVO> optionFlowTemplate = dataxFlowTemplateBO.findAllActive();
		 	request.setAttribute("optionFlowTemplate", optionFlowTemplate);  
		 	request.setAttribute("pageNo", pageNo); 
		 	request.setAttribute("flowIdSelect", flowIdSelect);  
		 	request.setAttribute("originateCopSelect", originateCopSelect);  
			request.setAttribute("eNameSelect", eNameSelect); 
			request.setAttribute("statusSelect", statusSelect); 
			
			request.setAttribute("dataxFlowVO", dataxFlowVO); 
			return getJspPath("company_flow_add");
	}
	
	 
	@Override
	protected String getBaseJspPath() {
		return JSP_PATH;
	}

}
